using System;
using System.Collections;
using Microsoft.Office.Interop.InfoPath.SemiTrust;
using System.Xml;
using InfoJetSoft.Service.Util;
using InfoJetSoft.Service.Entity;

namespace InfoJetSoft.Service.ManagedCode
{
	/// <summary>
	/// Summary description for XMLDOMNamedNodeMap.
	/// </summary>
	public class XMLDOMNamedNodeMap : IXMLDOMNamedNodeMap
	{
		private Form form = null;
		protected XmlNamedNodeMap xmlNodeMap = null;
		protected XmlNamespaceManager nsResolver = null;
		protected IEnumerator enumerator = null;

		public XMLDOMNamedNodeMap( Form frm, XmlNamedNodeMap nodeMap, XmlNamespaceManager resolver )
		{
			this.form = frm;
			this.xmlNodeMap = nodeMap;
			this.nsResolver = resolver;
			if( this.xmlNodeMap != null )
			{
                //CID 0628 //2010.07.17
                ArrayList enumList = new ArrayList();
                foreach (XmlAttribute attribute in this.xmlNodeMap)
                {
                    enumList.Add(XMLNode.createXMLNode(this.form, attribute, this.nsResolver));
                }
                this.enumerator = enumList.GetEnumerator();
                //CID 0628 //2010.07.17
			}
		}

		public XmlNamedNodeMap XmlNamedNodeMap
		{
			get
			{
				return this.xmlNodeMap;
			}
		}

		public static XMLDOMNamedNodeMap createXMLDOMNamedNodeMap( Form frm, XmlNamedNodeMap nodeMap, XmlNamespaceManager resolver )
		{
			if( nodeMap != null )
			{
				return new XMLDOMNamedNodeMap( frm, nodeMap, resolver );
			}
			else
			{
				return null;
			}
		}

		//IXMLDOMNamedNodeMap Interface Implementation

		public IEnumerator GetEnumerator()
		{
            return this.enumerator;
		}

        public IXMLDOMNode nextNode()
        {
            if (this.enumerator.MoveNext())
            {
                //CID 0628 //2010.07.17 //End
                return (IXMLDOMNode)this.enumerator.Current;
                //CID 0628 //2010.07.17 //End
            }
            else
            {
                return null;
            }
        }

        public void reset()
        {
            this.enumerator.Reset();
        }

		public IXMLDOMNode getNamedItem(string name)
		{
			XmlNode xmlNode = this.xmlNodeMap.GetNamedItem( name );
			return XMLNode.createXMLNode( this.form, xmlNode, this.nsResolver );
		}

		public IXMLDOMNode getQualifiedItem(string baseName, string namespaceURI)
		{
			XmlNode xmlNode = this.xmlNodeMap.GetNamedItem( baseName, namespaceURI );
			return XMLNode.createXMLNode( this.form, xmlNode, this.nsResolver );
		}

		public IXMLDOMNode removeNamedItem(string name)
		{
            //CID 0920 //2011.05.21 //Begin
            if (this.form.IsDomReadonly)
            {
                //InfoJet Service --> InfoBus Modified by Sunny 2013.04.07
                throw new InfoJetException("InfoBus cannot update the data, because the form is currently read-only.", "DOMNamedNodeMap.removeNamedItem");
            }
            //CID 0920 //2011.05.21 //End
            XmlNode removedXmlNode = this.xmlNodeMap.RemoveNamedItem(name);
			return XMLNode.createXMLNode( this.form, removedXmlNode, this.nsResolver );
		}
		public IXMLDOMNode removeQualifiedItem(string baseName, string namespaceURI)
		{
            //CID 0920 //2011.05.21 //Begin
            if (this.form.IsDomReadonly)
            {
                //InfoJet Service --> InfoBus Modified by Sunny 2013.04.07
                throw new InfoJetException("InfoBus cannot update the data, because the form is currently read-only.", "DOMNamedNodeMap.removeQualifiedItem");
            }
            //CID 0920 //2011.05.21 //End
            XmlNode removedXmlNode = this.xmlNodeMap.RemoveNamedItem(baseName, namespaceURI);
			return XMLNode.createXMLNode( this.form, removedXmlNode, this.nsResolver );
		}
		
		public int length 
		{ 
			get
			{ 
				return this.xmlNodeMap.Count; 
			} 
		}

		public IXMLDOMNode setNamedItem(IXMLDOMNode newItem)
		{
            //CID 0920 //2011.05.21 //Begin
            if (this.form.IsDomReadonly)
            {
                //InfoJet Service --> InfoBus Modified by Sunny 2013.04.07
                throw new InfoJetException("InfoBus cannot update the data, because the form is currently read-only.", "DOMNamedNodeMap.setNamedItem");
            }
            //CID 0920 //2011.05.21 //End
            if (newItem is IDOMNode)
			{
				IDOMNode newNode = (IDOMNode) newItem;
				XmlNode newXmlNode = this.xmlNodeMap.SetNamedItem( newNode.XmlNode );
				return XMLNode.createXMLNode( this.form, newXmlNode, this.nsResolver );
			}
			return null;
		}

		public IXMLDOMNode this[int index] 
		{ 
			get 
			{ 
				return XMLNode.createXMLNode( this.form, this.xmlNodeMap.Item( index ), this.nsResolver ); 
			} 
		}		

	}
}
